package com.mapzone.showmap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MapSurfaceActivity extends Activity {
	
	SurfaceView map_view;
	SurfaceHolder map_holder;

	private String map_doc_path;
	private Bitmap screen;
	
	DisplayMetrics dm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView( R.layout.map_surface );
		
		Intent intent = getIntent();
		map_doc_path = intent.getStringExtra( "path" );
		
		map_view = ( SurfaceView )findViewById( R.id.map_surface );
		map_holder = map_view.getHolder();	
		
		Log.v( "" , "" + map_view.getWidth() + " " + map_view.getHeight() );
		
		dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		
		init_ndk( dm.widthPixels, dm.heightPixels, dm.xdpi, dm.ydpi );
		open_map_doc( map_doc_path );
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		screen = null;
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
		
		if( null == screen )
		{
			screen = Bitmap.createBitmap( dm.widthPixels, dm.heightPixels, Config.ARGB_8888 );
		}
		
		draw_map( screen );
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	void draw_map( Bitmap bmp )
	{
		jni_draw( bmp, map_view.getWidth(), map_view.getHeight() );
		Canvas canvas = map_holder.lockCanvas( );
		Paint paint = new Paint();
		canvas.drawBitmap( bmp, 0, 0, paint );
		map_holder.unlockCanvasAndPost( canvas );
	}
	
	//////////////////Native callback//////////////////////////
	public void Invalidate( int flag )
	{
		draw_map( screen );
	}
	//////////////////Native method///////////////////////////

	public native void init_ndk( int pixelx, int pixely, float dpix, float dpiy );
	public native void init_mem_bitmap( Bitmap bitmap, int index );
	public native boolean open_map_doc( String map_path );
	public native void zoom_out(  );
	public native void zoom_in(  );
	public native void region_zoom(  );
	public native void scale_zoom( int scale );//这里传递的参数是缩放的倍数。
	public native void pan( );
	public native void pan( float x, float y);
	public native void select( );
	public native void clear_selection( );
	public native void entire();
	public native String[] get_object_att();
	
	public native void on_touch_down( float x, float y );
	public native void on_touch_move( float x, float y );
	public native void on_touch_up( float x, float y );
	public native void on_touch_double_click( float x, float y );
	public native void jni_draw( Bitmap screan_bitmap, float dpix, float dpiy  );
	public native void draw_test_bitmap( Bitmap bitmap );
	
	public native String[] get_layers();
	public native String[] get_fields( int layer_index );
	public native String[] get_values( int layer_index, int field_index );
	public native int query_bound( int value_index );
	
	public native int locate( double x, double y );
	public native int init_gps();
	public native int position_change( );
	
	public native void on_destroy();
	
	static
	{
		System.loadLibrary( "mz_sqlite" );
		System.loadLibrary( "foundation" );
		System.loadLibrary( "mathlib" );
		System.loadLibrary( "geos" );
		System.loadLibrary( "spatialreference" );
		System.loadLibrary( "geometry" );
		System.loadLibrary( "spatialdatabase" );
		System.loadLibrary( "gdl" );
		System.loadLibrary( "shared" );
		System.loadLibrary( "display" );
		System.loadLibrary( "symbol" );
		System.loadLibrary( "curl" );
		System.loadLibrary( "location" );
		System.loadLibrary( "carto" );
		System.loadLibrary( "undo" );
		System.loadLibrary( "oui" );
		System.loadLibrary( "mapzone_jni" );
		
		System.loadLibrary( "test_jni" );
	}

}
